package com.aliatry.base64;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * 原始base64编码
 *
 * @author Simon
 */
public class Base64Main {

    static String text = "Simon";

    /**
     * 原始base64编码
     */
    public static void original() throws Exception {
        BASE64Encoder encoder = new BASE64Encoder();
        BASE64Decoder decoder = new BASE64Decoder();

        byte[] textBytes = text.getBytes(StandardCharsets.UTF_8);

        // 编码
        String encode = encoder.encode(textBytes);
        System.out.println(encode);
        // 解码
        byte[] decodeBuffer = decoder.decodeBuffer(encode);
        String decode = new String(decodeBuffer, StandardCharsets.UTF_8);
        System.out.println(decode);
    }

    /**
     * jdk8 base64编码
     */
    public static void now() {
        Base64.Encoder encoder = Base64.getEncoder();
        Base64.Decoder decoder = Base64.getDecoder();

        byte[] textBytes = text.getBytes(StandardCharsets.UTF_8);

        // 编码
        String encode = encoder.encodeToString(textBytes);
        System.out.println(encode);
        // 解码
        byte[] decodeBytes = decoder.decode(encode);
        String decode = new String(decodeBytes, StandardCharsets.UTF_8);
        System.out.println(decode);
    }
}
